<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Mailing extends Admin_Controller {
	
	function __construct()
	{
		parent::__construct(6);
		$this->load->model('mailing_model', 'mailing');
	}

	function templates()
	{
		$vData = array(
			'list' => $this->mailing->getTemplates()
		);

		$this->getPage(array(
			"content" => $this->load->view('admin/mailing_templates', $vData, true),
			"title" => 'Шаблоны писем',
			'addJS' => array(
				'/js/tiny_mce/jquery.tinymce.js',
				'/js/tiny_mce/tiny_mce_settings.js'
			)
		));
	}

	function save()
	{
		if (empty($_POST) || empty($_POST['id']) || empty($_POST['pKey']) || empty($_POST['pTitle']) || empty($_POST['pText']))
			die();

		if ($this->db->where(array(
					'id' => (int)$_POST['id'],
					'pKey' => $_POST['pKey'],
					))->count_all_results('mail_template') != 1)
			die();

		echo (string)(int)$this->db->where(array(
					'id' => (int)$_POST['id'],
					'pKey' => $_POST['pKey'],
					))->update('mail_template', array(
						'pTitle' => $_POST['pTitle'],
						'pText' => $_POST['pText'],
						'dUpdated' => now(),
						'tUserUpdated' => $this->user->id
					));
	}

	function index()
	{
		$vData = array(
			'list' => $this->mailing->getMailings()
		);

		$this->getPage(array(
			"content" => $this->load->view('admin/mailings', $vData, true),
			"title" => 'Рассылки',
		));
	}

	function edit($id=0)
	{
		if ($id = abs($id))
			$vData = $this->mailing->getMailing($id);
		else
			$vData = array(
				'id' => $id
			);

		$this->getPage(array(
			"content" => $this->load->view('admin/mailing_edit', $vData, true),
			"title" => $id?'Редактирование рассылки':'Добавление рассылки',
			'addJS' => array(
				'/js/tiny_mce/jquery.tinymce.js',
				'/js/tiny_mce/tiny_mce_settings.js'
			)
		));
	}

	function saveMailing()
	{
		if (empty($_POST) || empty($_POST['pTitle']) || empty($_POST['pText']))
			die();

		$id = $this->mailing->saveMailing($_POST);

		redirect(base_url().'admin/mailing/edit/'.$id);
	}

	function process($id=0)
	{
		$vData = array(
			'mailing' => $this->mailing->getMailing($id)
		);

		if ($vData['mailing'])
		{
			$vData['metrix'] = $this->mailing->checkMailing($id);
			$content = $this->load->view('admin/mailing_process', $vData, true);
		}
		else
			$content = ' Вас не существует. Рассылки тоже.';

		$this->getPage(array(
			"content" => $content,
			"title" => 'Рассылка',
		));
	}

	function addSubscribes()
	{
		if (!empty($_POST) && !empty($_POST['emails']))
		{
			$this->mailing->addSubscribes($_POST['emails']);
			redirect(base_url().'admin/mailing/addSubscribes');
		}

		$this->getPage(array(
			"content" => $this->load->view('admin/mailing_add_subscribes', null, true),
			"title" => 'Добавление подписчиков',
		));
	}

	function subscriberList()
	{
		$vData = array(
			'list' => $this->mailing->getSubscribes()
		);

		$this->getPage(array(
			"content" => $this->load->view('admin/mailing_subscribes', $vData, true),
			"title" => 'Добавление подписчиков',
		));
	}

	function setMailing()
	{
		if (empty($_POST) || empty($_POST['id']) || !($id = abs($_POST['id'])))
			die();
		
		if ($res = $this->mailing->checkMailing($id))
		{
			dieJSON($res);
		}
		else
		{
			$res = $this->mailing->setMailing($id);

			if (is_array($res))
				dieJSON($res);
			else
				dieJSON(array('error' => 'Ошибка'));
		}
	}

	function check()
	{
		if (empty($_POST) || empty($_POST['id']) || !($id = abs($_POST['id'])))
			die();

		$res = $this->mailing->sendMoreMailing($id);

		if (is_array($res))
			dieJSON($res);
		else
			dieJSON(array('error' => 'Ошибка'));
	}

	function refresh($id=0)
	{
		if ($id = abs($id))
			$this->mailing->setMailing($id, true);
		else
			die('Что это?');

		redirect(base_url().'admin/mailing/process/'.$id);
	}
}
?>